<?php
class products_in_cart_handler extends Controller {
	
	public $viewData;
	
	function products_in_cart_handler() {
		parent::Controller();
		
		$this->load->library('cartpool');
		$this->load->library('session');
		
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
	}
	
	function index() {
	}
	
	function increaseQtyOnProduct($productId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->increaseQtyOnProduct($productId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}
	
	function decreaseQtyOnProduct($productId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->decreaseQtyOnProduct($productId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}

	function addCommentOnProduct($productId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->addCommentOnProduct($productId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}

	function updateCommentOnProduct($offset){
		if(isset($this->session->userdata['id'])){
			
			$productId = $_REQUEST['productId'];
			$comment = $_REQUEST['comment'];
			
			$this->cartpool->updateCommentOnProduct($productId, $comment);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}
	
	function deleteProductInCart($productId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->deleteProductInCart($productId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}
	
	function createProductInCart($offset){
		if(isset($this->session->userdata['id'])){
			
			$shoppingListId = $_REQUEST['shoppingListId'];
			$product = $_REQUEST['product'];
			$rayon = $_REQUEST['rayon'];
			
			$this->cartpool->createProductInCart($shoppingListId, $product, $rayon);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}		
	}
	
	function changePriceOnProduct($offset){
		if(isset($this->session->userdata['id'])){
			
			$productId = $_REQUEST['productId'];
			$price = $_REQUEST['price'];
			
			$this->cartpool->changePriceOnProduct($productId, $price);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata
			);
			$this->load->view('shopping_list', $this->viewData);
		}
	}

	function setImPaying($listId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->ImPayingForShoppingList($listId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}			
	}

	function closeShoppingList($listId, $offset){
		if(isset($this->session->userdata['id'])){
			$this->cartpool->closeShoppingList($listId);
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}			
	}

	function loadShoppingList($offset){
		if(isset($this->session->userdata['id'])){
			$this->viewData->cartpoolData = array(
				"shoppingList" => $this->cartpool->getShoppingList($offset),
				"sessionData" => $this->session->userdata			
			);
			$this->load->view('shopping_list', $this->viewData);
		}			
	}
}